<!--
 * @Author: Li Zengkun
 * @Date: 2022-09-23 10:58:56
 * @LastEditors: Li Zengkun
 * @LastEditTime: 2022-09-26 14:35:25
 * @Description: 
-->
<template>
  <div>
    <div class="left">
      <el-card shadow="hover" class="slide-box">
        <el-carousel>
          <el-carousel-item v-for="item in slideList" :key="item.id">
            <a :href="item.url" :target="item.target">
              <el-image :src="item.picture"></el-image>
            </a>
            <!-- <div class="slide-title">{{item.title}}</div> -->
          </el-carousel-item>
        </el-carousel>
      </el-card>
      <br />
      <el-card shadow="hover" ref="contentRef">
        <template #header>
          <el-button link type="primary">综合</el-button>
          <el-divider direction="vertical" />
          <el-button link>最新</el-button>
          <el-divider direction="vertical" />
          <el-button link>最热</el-button>
        </template>
        <ul>
          <li
            class="article"
            v-for="item in articleList"
            @click="toDetail(item.id)"
            :key="item.id"
          >
            <div class="text">
              <p class="tag">
                {{ item.cate_1st_name }}
                <el-divider direction="vertical" />
                {{ item.cate_2nd_name }}
                <el-divider direction="vertical" />
                {{ item.create_time }}
                <el-divider direction="vertical" v-if="item.tags.length != 0" />
                <el-tag v-for="tag in item.tags" :key="tag.id">{{
                  tag.name
                }}</el-tag>
              </p>
              <h3>{{ item.title }}</h3>
              <p>{{ item.description }}</p>
            </div>
            <div class="imgbox">
              <el-image :src="item.main_photo" class="article_photo"></el-image>
            </div>
          </li>
        </ul>
      </el-card>
      <div class="loading">
        <el-icon v-if="!isLoadFinish" class="rotate"><Loading /></el-icon>
        <span>
          {{ isLoadFinish ? "暂无更多内容..." : "加载中..." }}
        </span>
      </div>
    </div>

    <div class="right">
      <el-card shadow="hover" class="notice"> 公告 </el-card>
      <br />
      <el-card shadow="hover" class="notice">
        <ul>
          <li class="notice-box" v-for="item in stickyNotice" :key="item.id">
            <p class="time">
              {{ item.create_time }}
              <el-tag>置顶</el-tag>
            </p>
            <h3 class="title">
              <router-link :to="`/front-end/notice${item.id}`">
                {{ item.title }}
              </router-link>
            </h3>
          </li>
          <li class="notice-box" v-for="item in otherNotice" :key="item.id">
            <p class="time">
              {{ item.create_time }}
            </p>
            <h3 class="title">
              <router-link :to="`/front-end/notice${item.id}`">
                {{ item.title }}
              </router-link>
            </h3>
          </li>
        </ul>
      </el-card>
      <br />
      <img src="@/assets/img/common/ad.webp" alt="" />
    </div>
  </div>
</template>
 
<script setup>
import { ref, reactive, onMounted, computed } from "vue";
import { article, notice, slide } from "@/api";
import { useRouter, RouterLink } from "vue-router";

const router = useRouter();
const contentRef = ref(null);
let pageindex = 1;
const articleList = ref((await article.list({ pageindex })).data);
const slideList = (await slide.list()).data;

console.log(slideList);
/**
 * @description:
 * @return {*}
 * @param {*} id
 */
const toDetail = (id) => {
  router.push(`/front-end/article${id}`);
};

onMounted(() => {
  window.addEventListener("scroll", antiShake(lazyLoading, 100));
});
let isLock = false;
let isLoadFinish = ref(false);
const lazyLoading = async () => {
  // 可视区域高度
  let windowHeight = document.documentElement.clientHeight;
  //   滚动高度
  let scrollHeight = Math.max(
    document.documentElement.scrollTop,
    window.pageYOffset || 0
  );
  //   文档高度
  let documentHeight = document.documentElement.offsetHeight;
  //   是否触底
  let isBottom = windowHeight + scrollHeight >= documentHeight - 5;
  if (isBottom) {
    if (!isLock) {
      isLock = true;
      pageindex++;
      let { status, msg, data } = await article.list({ pageindex });
      if (data.length != 0) {
        articleList.value = articleList.value.concat(data);
      } else {
        isLoadFinish.value = true;
      }
      isLock = false;
    }
  }
};

/**
 * @description: 防抖
 * @return {*}
 * @param {*} fun
 * @param {*} wait
 */
const antiShake = (fun, wait) => {
  let timer = null;
  return function () {
    if (timer !== null) {
      clearTimeout(timer);
    }
    timer = setTimeout(fun, wait);
  };
};

const noticeList = ref((await notice.list({ pageindex: 1 })).data);

const stickyNotice = computed(() => {
  return noticeList.value.filter((item) => item.is_sticky === 1);
});
const otherNotice = computed(() => {
  return noticeList.value.filter((item) => item.is_sticky !== 1);
});
</script>

<style lang='less' scoped>
.el-card {
  border: none;
}
.left {
  width: 700px;
  .slide-box {
    --el-card-padding: 0;
    position: relative;
    .el-image {
      width: 100%;
    }
    .slide-title {
      position: absolute;
      top: 0;
      left: 0;
      text-align: center;
      color: #fff;
      height: 25%;
      background-image: linear-gradient(
        to bottom,
        rgba(0, 0, 0, 0.6),
        rgba(0, 0, 0, 0)
      );
      width: 100%;
    }
  }
  ul {
    list-style: none;
    padding: 0;
  }
  .article {
    width: 100%;
    padding: 20px 0px;
    display: flex;
    justify-content: space-between;
    cursor: pointer;
    &:hover {
      background: var(--el-color-info-light-9);
    }
    &:first-child {
      padding-top: 0;
    }
    &:not(:last-child) {
      border-bottom: 1px var(--el-menu-border-color) solid;
    }
    .text {
      width: 75%;
      .tag {
        font-size: 14px;
      }
      h3 {
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      p {
        color: var(--el-color-info);
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
    .imgbox {
      .article_photo {
        height: 80px;
        width: auto;
        vertical-align: middle;
      }
    }
  }
  .loading {
    text-align: center;
    padding: 10px 0;
  }
}
.right {
  width: 240px;
  // position: relative;
  position: fixed;
  left: calc((100vw - 960px) / 2 + 710px);
  img {
    width: 240px;
  }
  .notice {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
    width: 240px;
    .notice-box {
      padding-top: 10px;
      &:not(:last-child) {
        border-bottom: 1px var(--el-menu-border-color) solid;
        padding-bottom: 10px;
      }
      .time {
        font-size: 14px;
        color: var(--el-color-info);
      }
      .title {
        font-family: "alimama";
        a {
          color: var(--el-text-color-primary);
          text-decoration: none;
          &:hover {
            color: var(--el-color-primary);
            text-decoration: underline;
          }
        }
      }
    }
  }
}

.rotate {
  vertical-align: middle;
  animation: rotate 2s linear infinite;
}
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>

